package com.czxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.StuCourse;
import com.czxy.domain.Student;
import com.czxy.service.StuCourseService;
import com.czxy.service.StudentService;
import com.czxy.util.JWTUtil;
import com.czxy.vo.BaseResult;
import com.czxy.vo.StudentVo;
import io.jsonwebtoken.Claims;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //注入中间表的service
    @Autowired
    private StuCourseService stuCourseService;



    @PostMapping("/condition/{page}/{pagesize}")
    public BaseResult findByPage(@PathVariable("page") Integer page,
                                 @PathVariable("pagesize") Integer pagesize,
                                 @RequestBody StudentVo studentVo){
        Page<Student> studentPage = studentService.findByCondition(page, pagesize, studentVo);
        return BaseResult.ok(studentPage);
    }

    @PostMapping("/add")
    public BaseResult addStudent(@RequestBody Student student, HttpServletRequest request){
        String token = request.getHeader("authorization");
        Claims claims = JWTUtil.parseToken(token, "teacher");
        Integer tid = Integer.parseInt(claims.get("tid").toString());
        student.setCreateId(tid);

        String cityIds = student.getCityIdList().toString()
                .replaceAll(", ",",")
                .substring(1,21);
        student.setCityIds(cityIds);


        boolean flag = studentService.save(student);

        //添加选课信息
        if(student.getCourseIdList()!=null&&student.getCourseIdList().size()>0){
            for (Integer courseid:student.getCourseIdList()){
                StuCourse stuCourse = new StuCourse(student.getStuid(),courseid,0);
                stuCourseService.save(stuCourse);
            }
        }




        if (flag) return BaseResult.ok();
        else return BaseResult.error();
    }

    /**
     * 1 学生基本信息的更新：姓名、年龄、性别、班级、生日
     * 2 学生基本信息：省市区
     * 3 更新学生选课信息：
     *
     * @param student
     * @param request
     * @return
     */
    @PutMapping("/update")
    public BaseResult updateStudent(@RequestBody Student student, HttpServletRequest request){
        //1 更新学生的基本信息
        String cityIds = student.getCityIdList().toString()
                .replaceAll(", ",",")
                .substring(1,21);
        student.setCityIds(cityIds);

        studentService.updateById(student);

//        * 3 更新学生选课信息：
        //3.1 根据学生的stuid删除学生所有的选课信息
        QueryWrapper<StuCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stuid",student.getStuid());
        stuCourseService.remove(queryWrapper);
        //3.2 重新插入
        for (Integer courseId:student.getCourseIdList()){
            StuCourse stuCourse = new StuCourse(student.getStuid(), courseId, 0);
            stuCourseService.save(stuCourse);
        }

        return BaseResult.ok();
    }


    @DeleteMapping("/{stuid}")
    public BaseResult deleteById(@PathVariable Integer stuid){
        //方式一：假删除
//        Student stu = studentService.getById(stuid);
//        stu.setIsDelete(1);
//        studentService.updateById(stu);

        //方式二：真删除
        //删除学生选课信息
        QueryWrapper<StuCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stuid",stuid);
        stuCourseService.remove(queryWrapper);
        //删除学生信息
        studentService.removeById(stuid);


        return BaseResult.ok();

    }

}